<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <title>地图单击拾取经纬度</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge">
    <style>
        #container {
            overflow: hidden;
            width: 100%;
            height: 600px;
            margin: 0;
            font-family: "微软雅黑";
        }
        #r-result{width:100%;position: absolute;}
        #r-result input{
            z-index: 999;
            width: auto;
            min-width: 22rem;
            padding: .75rem 1.25rem;
            margin-left: 1.25rem;
            position: fixed;
            top: 1rem;
            background-color: #fff;
            border-radius: .25rem;
            font-size: 14px;
            color: #666;
            border-color: rgba(27, 142, 236, 0.5);
            
        }
        #r-result input:focus{
            border-color: rgba(27, 142, 236, 0.5) ;
            box-shadow: 0 2px 6px 0 rgba(27, 142, 236, 0.5);
            outline: none;
        }
        .tangram-suggestion-main{
            z-index: 99999;
        }
    </style>
    <script type="text/javascript" src="__SKIN__/js/jquery.js?v={$version}"></script>
    <script src="//api.map.baidu.com/api?type=webgl&v=1.0&ak=VMDxhX1vXvhvUtXfKCyReHPXXRgh9r3j"></script>
</head>
<body style="position: relative;">
<div id="container"></div>
<div id="r-result"><input type="text" id="suggestId" size="20" value="" style="width:150px;" placeholder="搜索地点" /></div>
<div id="searchResultPanel" style="border:1px solid #C0C0C0;width:150px;height: auto;display: none;" ></div>
</body>
</html>
<script>
    var point = $("#point", parent.document.body).val();

    var map = new BMapGL.Map('container');



    if ('' == point || !point){
        var geolocation = new BMapGL.Geolocation();
        geolocation.getCurrentPosition(function(r){
            if(this.getStatus() == BMAP_STATUS_SUCCESS){
                var mk = new BMapGL.Marker(r.point);
                map.addOverlay(mk);
                map.panTo(r.point);

                map.centerAndZoom(new BMapGL.Point(r.point.lng, r.point.lat), 15);

                $("#point_hidden", parent.document.body).val(r.point.lng+','+r.point.lat);
                // alert('您的位置：' + r.point.lng + ',' + r.point.lat);
            }else{
                map.centerAndZoom(new BMapGL.Point(116.404, 39.928), 15);
            }
        });
    }else{
        var point_Arr = point.split(',');
        map.centerAndZoom(new BMapGL.Point(point_Arr[0],point_Arr[1]), 15);

        var mk = new BMapGL.Marker( new BMapGL.Point(point_Arr[0],point_Arr[1]));
        map.addOverlay(mk);
    }
    map.enableScrollWheelZoom(true);

    map.addEventListener('click', function (e) {
        map.clearOverlays();

        var mk = new BMapGL.Marker(e.latlng);
        map.addOverlay(mk);
        $("#point_hidden", parent.document.body).val(e.latlng.lng+','+e.latlng.lat);

        // alert('点击位置经纬度：' + e.latlng.lng + ',' + e.latlng.lat);
    });


    // 百度地图API功能
    function G(id) {
        return document.getElementById(id);
    }

    var ac = new BMapGL.Autocomplete(    //建立一个自动完成的对象
        {"input" : "suggestId"
            ,"location" : map
        });

    ac.addEventListener("onhighlight", function(e) {  //鼠标放在下拉列表上的事件
        var str = "";
        var _value = e.fromitem.value;
        var value = "";
        if (e.fromitem.index > -1) {
            value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
        }
        str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;

        value = "";
        if (e.toitem.index > -1) {
            _value = e.toitem.value;
            value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
        }
        str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
        G("searchResultPanel").innerHTML = str;
    });

    var myValue;
    ac.addEventListener("onconfirm", function(e) {    //鼠标点击下拉列表后的事件
        var _value = e.item.value;
        myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
        G("searchResultPanel").innerHTML ="onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;

        setPlace();
    });

    function setPlace(){
        map.clearOverlays();    //清除地图上所有覆盖物
        function myFun(){
            var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
            map.centerAndZoom(pp, 18);
            map.addOverlay(new BMapGL.Marker(pp));    //添加标注
            $("#point_hidden", parent.document.body).val(pp.lng+','+pp.lat);
        }
        var local = new BMapGL.LocalSearch(map, { //智能搜索
            onSearchComplete: myFun
        });
        local.search(myValue);
    }
</script>